package com.wwwarehouse.carddesk.fragment.teamfragment.checkdownclassfragemnt;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.teambean.RecyclePowerBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.carddesk.eventbus_event.TeamPREvent;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by xiaoke.ni on 2017/10/11.
 */
public class TeamPRSuccessFragment extends BaseFragment implements View.OnClickListener {

    private View mView;

    private StateLayout  mStateLayout;
    private ImageView    mPrSuccessIv;
    private TextView     mPrAdministratorSuccessTv;
    private TextView     mLine1Tv;
    private TextView     mLine2Tv;
    private Button       mPrSeeNotSuccessBtn;
    private LinearLayout mPrSuccessBtnLl;
    private Button       mNextBtn;
    private Button       mFinishBtn;
    private Button       mPrErrorBtn;


    private ArrayList<Integer> mCardIds;
    private String             mBusinessId;
    private String             mBeUserId;
    private RecyclePowerBean mRecyclePowerBean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getActivity(), R.layout.fragment_pr_success_team, null);
        return mView;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.pr_next_btn) {
            ((BaseCardDeskActivity) mActivity).showBackBt();
            popFragment();
            EventBus.getDefault().post(new TeamPREvent("pr_refresh"));
        } else if (id == R.id.pr_finish_btn) {
            ((BaseCardDeskActivity) mActivity).showBackBt();
            EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
        } else if (id == R.id.pr_error_btn) {
            ((BaseCardDeskActivity) mActivity).showBackBt();
            popFragment();
            EventBus.getDefault().post(new TeamPREvent("pr_refresh"));
        } else if (id == R.id.pr_see_not_success_btn) {
            if (mRecyclePowerBean.getDetails() != null) {
                ((BaseCardDeskActivity) mActivity).showBackBt();
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("details", (ArrayList<? extends Parcelable>) mRecyclePowerBean.getDetails());
                TeamPRUncollectedDetailFragment teamPRUncollectedDetailFragment = new TeamPRUncollectedDetailFragment();
                teamPRUncollectedDetailFragment.setArguments(bundle);
                pushFragment(teamPRUncollectedDetailFragment);
            }
/*((BaseCardDeskActivity) mActivity).showBackBt();
            PRUncollectedDetailFragment prUncollectedDetailFragment = new PRUncollectedDetailFragment();
            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("details", (ArrayList<? extends Parcelable>) retrieveAuthorityBean.getDetails());
            prUncollectedDetailFragment.setArguments(bundle);
            pushFragment(prUncollectedDetailFragment);*/
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof TeamPRSuccessFragment) {
            mActivity.setTitle(getString(R.string.teamdesk_recycle_power));
        }
    }

    @Override
    public void requestDatas() {
        initView();
        Bundle bundle = getArguments();
        if (bundle != null) {
            mCardIds = bundle.getIntegerArrayList("cardIds");
            mBusinessId = bundle.getString("businessId");
            mBeUserId = bundle.getString("beUserId");
        }
        ((BaseCardDeskActivity) mActivity).hideBackBt();
        retrieveAuthority();
    }

    private void initView() {
        mStateLayout = (StateLayout) mView.findViewById(R.id.slt);
        mPrSuccessIv = (ImageView) mView.findViewById(R.id.pr_success_iv);
        mPrAdministratorSuccessTv = (TextView) mView.findViewById(R.id.pr_administrator_success_tv);
        mLine1Tv = (TextView) mView.findViewById(R.id.line1_tv);
        mLine2Tv = (TextView) mView.findViewById(R.id.line2_tv);
        mPrSeeNotSuccessBtn = (Button) mView.findViewById(R.id.pr_see_not_success_btn);
        mPrSuccessBtnLl = (LinearLayout) mView.findViewById(R.id.pr_success_btn_ll);
        mNextBtn = (Button) mView.findViewById(R.id.pr_next_btn);
        mFinishBtn = (Button) mView.findViewById(R.id.pr_finish_btn);
        mPrErrorBtn = (Button) mView.findViewById(R.id.pr_error_btn);
        mPrSeeNotSuccessBtn.setOnClickListener(this);
        mNextBtn.setOnClickListener(this);
        mFinishBtn.setOnClickListener(this);
        mPrErrorBtn.setOnClickListener(this);
    }

    /**
     * 权限回收-回收权限
     */
    private void retrieveAuthority() {
        Map<String, Object> recycleMap = new HashMap<>();
        recycleMap.put("beUserId", mBeUserId);
        recycleMap.put("businessId", mBusinessId);
        recycleMap.put("cardIds", mCardIds);
        mStateLayout.showProgressView(true);
        NoHttpUtils.httpPost(CardDeskConstant.BATCH_RETRIEVE_AUTHORITY, recycleMap, onResponseListener, 0);
    }


    private NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            try {
                switch (what) {
                    case 0:
                        if (TextUtils.equals("0", commonClass.getCode())) {
                            mRecyclePowerBean = JSON.parseObject(commonClass.getData().toString(), RecyclePowerBean.class);
                            int status = Integer.parseInt(mRecyclePowerBean.getStatus());
                            /**
                             * nameDesc : [""]
                             * details : [{"cardName":"","faileReason":"未知原因","userName":"王海海"}]
                             * user : 王海海
                             * status : 1
                             */
                            //回收结果.0-成功 1-失败 2-部分失败 3-异常
                            switch (status) {
                                case 0:
                                    mPrSuccessIv.setImageResource(R.drawable.icon_success);
                                    mPrAdministratorSuccessTv.setText(getString(R.string.teamdesk_user_pr_success));
                                    if (mRecyclePowerBean.getNameDesc() != null && mRecyclePowerBean.getNameDesc().size() > 0) {
                                        if (mRecyclePowerBean.getNameDesc().size() > 1) {
                                            mLine1Tv.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_recycle_powers_success), mRecyclePowerBean.getNameDesc().get(0), mRecyclePowerBean.getNameDesc().size()));
                                        } else {
                                            mLine1Tv.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_recycle_power_success), mRecyclePowerBean.getNameDesc().get(0)));
                                        }
                                    }
                                    if (mRecyclePowerBean.getUser() != null) {
                                        mLine2Tv.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_recycle_name_success), mRecyclePowerBean.getUser()));
                                    }
                                    mPrSuccessBtnLl.setVisibility(View.VISIBLE);
                                    mPrErrorBtn.setVisibility(View.GONE);
                                    mPrSeeNotSuccessBtn.setVisibility(View.GONE);
                                    break;
                                case 1:
                                    mPrSuccessIv.setImageResource(R.drawable.icon_failure);
                                    mPrAdministratorSuccessTv.setText(getString(R.string.teamdesk_user_pr_recycling_fail));
                                    if (mRecyclePowerBean.getNameDesc() != null && mRecyclePowerBean.getNameDesc().size() > 0) {
                                        if (mRecyclePowerBean.getNameDesc().size() > 1) {
                                            mLine1Tv.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_recycle_powers_failed), mRecyclePowerBean.getNameDesc().get(0), mRecyclePowerBean.getNameDesc().size()));
                                        } else {
                                            mLine1Tv.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_recycle_power_failed), mRecyclePowerBean.getNameDesc().get(0)));
                                        }
                                    }
                                    mLine2Tv.setText("");
                                    mPrSuccessBtnLl.setVisibility(View.VISIBLE);
                                    mPrErrorBtn.setVisibility(View.GONE);
                                    mPrSeeNotSuccessBtn.setVisibility(View.VISIBLE);
                                    break;
                                case 2:
                                    mPrSuccessIv.setImageResource(R.drawable.icon_success);
                                    mPrAdministratorSuccessTv.setText(getString(R.string.teamdesk_user_pr_part_recycling_success));
                                    if (mRecyclePowerBean.getNameDesc() != null && mRecyclePowerBean.getNameDesc().size() > 0) {
                                        if (mRecyclePowerBean.getNameDesc().size() > 1) {
                                            mLine1Tv.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_recycle_powers_success), mRecyclePowerBean.getNameDesc().get(0), mRecyclePowerBean.getNameDesc().size()));
                                        } else {
                                            mLine1Tv.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_recycle_power_success), mRecyclePowerBean.getNameDesc().get(0)));
                                        }
                                    }
                                    if (mRecyclePowerBean.getUser() != null) {
                                        mLine2Tv.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_recycle_name_success), mRecyclePowerBean.getUser()));
                                    }
                                    mPrSuccessBtnLl.setVisibility(View.VISIBLE);
                                    mPrErrorBtn.setVisibility(View.GONE);
                                    mPrSeeNotSuccessBtn.setVisibility(View.VISIBLE);
                                    break;
                                case 3:
                                    mPrSuccessIv.setImageResource(R.drawable.icon_failure);
                                    mPrAdministratorSuccessTv.setText(getString(R.string.teamdesk_permission_notfound_txt));
                                    if (mRecyclePowerBean.getNameDesc() != null && mRecyclePowerBean.getNameDesc().size() > 0) {
                                        if (mRecyclePowerBean.getNameDesc().size() > 1) {
                                            mLine1Tv.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_permissions_description_txt_1), mRecyclePowerBean.getNameDesc().get(0)));
                                        } else {
                                            mLine1Tv.setText(String.format(mActivity.getResources().getString(R.string.teamdesk_permission_description_txt_1), mRecyclePowerBean.getNameDesc().get(0)));
                                        }
                                    }
                                    mLine2Tv.setText(getString(R.string.teamdesk_permission_description_txt_2));
                                    mPrSuccessBtnLl.setVisibility(View.VISIBLE);
                                    mPrErrorBtn.setVisibility(View.GONE);
                                    mPrSeeNotSuccessBtn.setVisibility(View.GONE);
                                    break;
                            }
                        } else {
                            mStateLayout.showEmptyView(commonClass.getMsg(), true);
                            mStateLayout.setEmptyListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    retrieveAuthority();
                                }
                            });
                        }
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }

        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    retrieveAuthority();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };
}
